/**
    This file is part of no-more-denormals.

    no-more-denormals is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    no-more-denormals is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with no-more-denormals.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "nmd_set.h"

#ifdef _WIN32
#include <xmmintrin.h>
#endif

#include <assert.h>

void nmdSet(nmdSavedState* pSaveInHere)
{
	assert(pSaveInHere);
#ifdef _WIN32
	// FPU
	pSaveInHere->mFPMode = _controlfp(_CW_DEFAULT, 0xffffffff);
	_controlfp(_DN_FLUSH, _MCW_DN);	
	// SSE
	pSaveInHere->mSSEMode = _mm_getcsr();
	int	lNewMode = pSaveInHere->mSSEMode | 0x0804;
	__mm_setcsr(lNewMode)
#elif MAC
	fegetenv(&(pSaveInHere->mEnv));
	fesetenv(FE_DFL_DISABLE_SSE_DENORMS_ENV);
#endif
	// TODO linux?
}

void nmdRestore(nmdSavedState* pRestoreFromHere)
{
	assert(pRestoreFromHere);
#ifdef _WIN32
	_controlfp(pRestoreFromHere->mFPMode, 0xffffffff)
	__mm_setcsr(pRestoreFromHere->mSSEMode);
#elif MAC
	fesetenv(&(pRestoreFromHere->mEnv));
#endif
	// TODO linux?
}

